package org.budo.mongo.filter.metric;

import org.budo.graph.annotation.SpringGraph;
import org.budo.graph.metric.MetricUtil;
import org.budo.mongo.filter.AbstractMongoFilter;
import org.budo.mongo.filter.MongoFilterChain;

import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.WriteResult;

/**
 * @author limingwei
 * @see com.alibaba.druid.support.http.WebStatFilter
 */
public class MongoMetricFilter extends AbstractMongoFilter {
    @SpringGraph
    @Override
    public DBCollection db_getCollection(MongoFilterChain filterChain, DB db, String $0) {
        String key = "mongo." + db.getName() + "." + $0 + ".db_getCollection";
        String uuid = MetricUtil.before(key, true);

        DBCollection db_getCollection = super.db_getCollection(filterChain, db, $0);

        MetricUtil.after(key, uuid);
        return db_getCollection;
    }

    @SpringGraph
    @Override
    public long dbCollection_count(MongoFilterChain filterChain, DBCollection dbCollection, DBObject $0) {
        String key = "mongo." + dbCollection.getDB().getName() + "." + dbCollection.getName() + ".dbCollection_count";
        String uuid = MetricUtil.before(key, true);

        long dbCollection_count = super.dbCollection_count(filterChain, dbCollection, $0);

        MetricUtil.after(key, uuid);
        return dbCollection_count;
    }

    @SpringGraph
    @Override
    public DBCursor dbCollection_find(MongoFilterChain filterChain, DBCollection dbCollection, DBObject $0) {
        String key = "mongo." + dbCollection.getDB().getName() + "." + dbCollection.getName() + ".dbCollection_find";
        String uuid = MetricUtil.before(key, true);

        DBCursor dbCollection_find = super.dbCollection_find(filterChain, dbCollection, $0);

        MetricUtil.after(key, uuid);
        return dbCollection_find;
    }

    @SpringGraph
    @Override
    public DBCursor dbCollection_find(MongoFilterChain filterChain, DBCollection dbCollection, DBObject $0, DBObject $1) {
        String key = "mongo." + dbCollection.getDB().getName() + "." + dbCollection.getName() + ".dbCollection_find_projection";
        String uuid = MetricUtil.before(key, true);

        DBCursor dbCollection_find = super.dbCollection_find(filterChain, dbCollection, $0, $1);

        MetricUtil.after(key, uuid);
        return dbCollection_find;
    }

    @SpringGraph
    @Override
    public DBObject dbCollection_findOne(MongoFilterChain filterChain, DBCollection dbCollection, DBObject $0, DBObject $1) {
        String key = "mongo." + dbCollection.getDB().getName() + "." + dbCollection.getName() + ".dbCollection_findOne";
        String uuid = MetricUtil.before(key, true);

        DBObject dbCollection_findOne = super.dbCollection_findOne(filterChain, dbCollection, $0, $1);

        MetricUtil.after(key, uuid);
        return dbCollection_findOne;
    }

    @SpringGraph
    @Override
    public WriteResult dbCollection_insert(MongoFilterChain filterChain, DBCollection dbCollection, DBObject[] $0) {
        String key = "mongo." + dbCollection.getDB().getName() + "." + dbCollection.getName() + ".dbCollection_insert";
        String uuid = MetricUtil.before(key, true);

        WriteResult dbCollection_insert = super.dbCollection_insert(filterChain, dbCollection, $0);

        MetricUtil.after(key, uuid);
        return dbCollection_insert;
    }

    @SpringGraph
    @Override
    public WriteResult dbCollection_save(MongoFilterChain filterChain, DBCollection dbCollection, DBObject $0) {
        String key = "mongo." + dbCollection.getDB().getName() + "." + dbCollection.getName() + ".dbCollection_save";
        String uuid = MetricUtil.before(key, true);

        WriteResult dbCollection_save = super.dbCollection_save(filterChain, dbCollection, $0);

        MetricUtil.after(key, uuid);
        return dbCollection_save;
    }

    @SpringGraph
    @Override
    public DB mongoClient_getDB(MongoFilterChain filterChain, MongoClient mongoClient, String $0) {
        String key = "mongo." + $0 + ".mongoClient_getDB";
        String uuid = MetricUtil.before(key, true);

        DB mongoClient_getDB = super.mongoClient_getDB(filterChain, mongoClient, $0);

        MetricUtil.after(key, uuid);
        return mongoClient_getDB;
    }

    @SpringGraph
    @Override
    public boolean dbCursor_hasNext(MongoFilterChain filterChain, DBCursor dbCursor) {
        return super.dbCursor_hasNext(filterChain, dbCursor);
    }

    @SpringGraph
    @Override
    public DBCursor dbCursor_limit(MongoFilterChain filterChain, DBCursor dbCursor, int $0) {
        return super.dbCursor_limit(filterChain, dbCursor, $0);
    }

    @SpringGraph
    @Override
    public DBCursor dbCursor_sort(MongoFilterChain filterChain, DBCursor dbCursor, DBObject $0) {
        return super.dbCursor_sort(filterChain, dbCursor, $0);
    }

    @SpringGraph
    @Override
    public void dbCursor_close(MongoFilterChain filterChain, DBCursor dbCursor) {
        super.dbCursor_close(filterChain, dbCursor);
    }

    @SpringGraph
    @Override
    public DBObject dbCursor_next(MongoFilterChain filterChain, DBCursor dbCursor) {
        return super.dbCursor_next(filterChain, dbCursor);
    }

    @SpringGraph
    @Override
    public DBCursor dbCursor_copy(MongoFilterChain filterChain, DBCursor dbCursor) {
        return super.dbCursor_copy(filterChain, dbCursor);
    }

    @SpringGraph
    @Override
    public CommandResult db_command(MongoFilterChain filterChain, DB db, DBObject $0) {
        String key = "mongo." + db.getName() + ".db_command";
        String uuid = MetricUtil.before(key, true);

        CommandResult db_command = super.db_command(filterChain, db, $0);

        MetricUtil.after(key, uuid);
        return db_command;
    }
}